package com.microtf.inscription.framework.service;

import com.microtf.inscription.framework.service.excelXml.ReadEvent;
import com.microtf.inscription.framework.service.excelXml.XmlReaderHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;

/**
 * Excel文件读取功能
 * 读取excel格式后通过回调方法进行回调{@link ReadEvent#onData}
 * @since 1.0
 * @author guliuzhong
 */
@Service
@Slf4j
public class ExcelReaderService extends BaseService {
    /**
     * 读取xml格式的excel
     * <p>excel文件保存方法，另存选择xml</p>
     *
     * @author guliuzhong
     * @param inputStream 文件流
     * @param readEvent 事件回调接口
     */
    public void parseXmlExcel(InputStream inputStream,ReadEvent readEvent)  {
        SAXParserFactory saxParserFactory = SAXParserFactory.newDefaultInstance();
        try (XmlReaderHandler xmlReaderHandler = new XmlReaderHandler(readEvent)) {
            SAXParser saxParser = saxParserFactory.newSAXParser();
            saxParser.parse(inputStream, xmlReaderHandler);
        } catch (Exception e) {
            log.error("解析excel出错{}",e.getMessage());
        }
    }
}
